import * as Cesium from 'cesium';
export default function initViewer() {
  //设置cesium token
  Cesium.Ion.defaultAccessToken =
    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0MGNkNzY3YS1mMDcxLTQ0NjQtOTlhMC00Y2Y0MjA3ZGQ1ZTIiLCJpZCI6MjY0NzQyLCJpYXQiOjE3MzUwOTQ0NzF9.09AnFzH3aVpi7s-0WqcAYuf69lr4UUIbKL4DmKBf1qg';

  //设置根目录静态资源 /代表根目录public
  window.CESIUM_BASE_URL = '/';

  //设置Cesium默认视角
  Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(
    //西边的纬度
    89.5,
    //南边的纬度
    20.4,
    //东边经度
    110.4,
    //北边纬度
    61.2
  );

  const viewer = new Cesium.Viewer('cesiumContainer', {
    // 是否显示信息窗口
    infoBox: true,
    //右上角搜索框显示隐藏
    geocoder: false,
    //右上角home切换返回地球模型
    homeButton: false,
    //右上角场景2D3D切换按钮显示隐藏
    sceneModePicker: false,
    //右上角是否显示图层模式切换显示隐藏
    baseLayerPicker: false,
    //右上角帮助提示按钮显示隐藏
    navigationHelpButton: false,
    //左下角是否播放动画指针
    animation: false,
    //下方时间轴
    timeline: false,
    //右下角全屏按钮
    fullscreenButton: false,
  });

  //隐藏底部logo
  viewer.cesiumWidget.creditContainer.style.display = 'none';

  //添加3D瓦片建筑
  // let tiles3d = await Cesium.createOsmBuildingsAsync();
  // const osmBuildings = viewer.scene.primitives.add(tiles3d);

  //设置沙箱允许使用js
  var iframe = document.getElementsByClassName('cesium-infoBox-iframe')[0];
  iframe.setAttribute(
    'sandbox',
    'allow-same-origin allow-scripts allow-popups allow-forms'
  );
  iframe.setAttribute('src', '');

  //广州塔
  var postion = Cesium.Cartesian3.fromDegrees(113.3191, 23.109, 1000);
  viewer.camera.flyTo({
    destination: postion,
    duration: 2,
  });

  return viewer;
}
